import {
  Sysmsg
} from '../../model/sysmsg-model.js';

var sysmsg = new Sysmsg();
import {
  Company
} from '../../model/company-model.js';
var company = new Company();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    msgType: 0,
  },
  // bindtap="toMsgDet" bindlongtap="myLongTap"
  myTouchStart(e) {
    this.setData({ startTimeStamp: e.timeStamp });
  },
  myTouchEnd(e) {
    if (e.timeStamp - this.data.startTimeStamp > 350) {
      // 触摸时间超过350毫秒，触发长按逻辑
    } else {
      // 未超过350毫秒，触发单击逻辑（如调用 myTap 函数）
      this.myTap(e);
    }
  },
  myLongTap(e) {
    this.delComment(e);
  },
  myTap(e) {
    this.toMsgDet(e);
  },
  delComment:function(e)
  {

    var that = this;

    var id = e.currentTarget.dataset.id;
    var params = {id:id};
    console.log(params);
    wx.showModal({
      title: '提示',
      content: '确定删除此条消息？',
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          sysmsg.sendDel((data) => {
            console.log(data);
            if(data.code == 1 )
            {
              wx.showToast({
                title: data.msg,
                icon: 'none',
                duration: 2000
              })
              that.onShow();
            }else{
              wx.showToast({
                title: data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          },params);
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    wx.setNavigationBarTitle({
      title: '消息中心',
    })
    let ctoken = wx.getStorageSync('ctoken')
    if (ctoken) {
      this.setData({
        msgType: 1
      })
    }
    that.initpage();

  },

  onImageError(e) {
    const index = e.currentTarget.dataset.index; // 哪个图片出错
  },

  toMsgDet(e) {
    const id = e.currentTarget.dataset.id;
    const enterprise_id = e.currentTarget.dataset.enterprise
    const job_seeker_id = e.currentTarget.dataset.jobseeker
    const enterprise_name =e.currentTarget.dataset.enterprisename
    wx.navigateTo({
      url: `/pages/msgdet/index?id=${id}&enterprise_id=${enterprise_id}&job_seeker_id=${job_seeker_id}&enterprise_name=${enterprise_name}`
    });
  },


  verifyCompanyLogin(successCallback, failCallback) {
    const ctoken = wx.getStorageSync('ctoken');
    if (!ctoken) {
      if (failCallback) failCallback();
      return;
    }

    company.checkLogin((res) => {
      if (res.error == 0) {
        if (successCallback) successCallback(res);
      } else {
        console.warn('[verifyCompanyLogin] 企业验证失败，错误码:', res.error);
        // 清理 ctoken
        wx.removeStorageSync('ctoken');
        if (failCallback) failCallback(res);
      }
    }, {
      ctoken
    });
  },

  initpage: function () {

    var that = this;

    this.verifyCompanyLogin(() => {
      var params = {
        page: that.data.page,
        limit: 10,
      };
      let ctoken = wx.getStorageSync('ctoken')
      if (ctoken) {
        params.ctoken = ctoken
      }
      sysmsg.newGetsysmsgList((data) => {

        var sysmsglist = data.data.data;

        that.setData({
          sysmsglist: sysmsglist
        });
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh();
      }, params);
    }, () => {
      var params = {
        page: that.data.page,
        limit: 10,
      };
      sysmsg.newGetsysmsgList((data) => {
        var sysmsglist = data.data.data;
        that.setData({
          sysmsglist: sysmsglist
        });
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh();
      }, params);
    })



  },
  toSysmsg() {
    wx.navigateTo({
      url: '/pages/systongzhi/index'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 每次返回页面时重新拉取第一页数据
    this.setData({
      page: 1
    });
    this.initpage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.onLoad();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page = this.data.page + 1;
    that.initPage();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '消息中心',
      path: '/pages/sysmsg/index'
    }
  },

})
